Page({
  data: {
    classroomNumber: '',
    building: '',
    room: '',
    date: '',
    time: '',
    expireTime: '',
    serialNumber: '', // 编号
  },

  onLoad(options) {
    const classroomInfo = wx.getStorageSync('classroomInfo');
    const userInfo = wx.getStorageSync('userInfo');
    if (!classroomInfo || !userInfo) return;

    // 处理教学楼 和 教室号
    const fullClassroom = classroomInfo.classroomNumber;
    const building = fullClassroom.substring(0, 2);
    const room = fullClassroom.substring(2);

    // 处理日期
    const today = new Date();
    let actualDate = today;
    if (classroomInfo.date === '次日') {
      actualDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1);
    }
    const dateStr = `${actualDate.getFullYear()}/${(actualDate.getMonth() + 1).toString().padStart(2, '0')}/${actualDate.getDate().toString().padStart(2, '0')}`;
    const dateSerialStr = `${actualDate.getFullYear()}${(actualDate.getMonth() + 1).toString().padStart(2, '0')}${actualDate.getDate().toString().padStart(2, '0')}`; // 用于编号

    // 处理验证码有效时间
    let endTimeStr = classroomInfo.time.split('-')[1];
    let [endHour, endMinute] = endTimeStr.split(':').map(Number);
    const expireDate = new Date(actualDate);
    expireDate.setHours(endHour);
    expireDate.setMinutes(endMinute - 10);
    expireDate.setSeconds(0);

    const expireTimeStr = `${expireDate.getFullYear()}/${(expireDate.getMonth() + 1).toString().padStart(2, '0')}/${expireDate.getDate().toString().padStart(2, '0')} ${expireDate.getHours().toString().padStart(2, '0')}:${expireDate.getMinutes().toString().padStart(2, '0')}`;

    // 生成编号
    const serialNumber = dateSerialStr + building + room + userInfo.userNumber;

    this.setData({
      classroomNumber: fullClassroom,
      building,
      room,
      date: dateStr,
      time: classroomInfo.time,
      expireTime: expireTimeStr,
      serialNumber
    });
  }
})